/**
 * Created by Administrator on 2017/5/11.
 */

//红包测试    算法
const red = require('../../app_server/models/RedBag')
const pipe = require('../../app_server/models/Pipe')
// 红包金额总量


const test = {
    argorith: function () {

        var total = 10;
        var totalRemain = 10;
// 红包个数
        var size = 10;
        var sizeRemain = 10
// 测试次数
        var times = 200;

        var result = [];
        for (var j = 0; j < times; j++) {
            var list = [];
            totalRemain = total;
            sizeRemain = size
            var s = 0
            for (var i = 0; i < size; i++) {
                var money = getRandomMoney();
                list.push(money)
                s += money;
            }
            console.log('one row ' + s)
            result.push(list)
        }
        console.log('======result========');
        console.log(result);

        var average = [];
        var rows = result.length;
        var cols = result[0].length;
        for (var j = 0; j < cols; j++) {
            var temp = 0
            for (var i = 0; i < rows; i++) {
                temp += result[i][j];
            }
            average.push(temp)
        }
        console.log('========average======');
        console.log(average);
        function getRandomMoney() {
            var remainSize = sizeRemain;
            var remainMoney = totalRemain;
            if (remainSize == 1) {
                remainSize--;
                // callback
                // console.log('shengyu0 '+remainMoney)
                return Math.round(remainMoney * 100) / 100
                // return remainMoney
            }
            var min = 0.01;
            var max = remainMoney / remainSize * 2;

            var random = Math.random() * max;
            var money = random <= min ? 0.01 : random;
            money = Math.floor(money * 100) / 100;

            remainSize--;
            remainMoney -= money;
            totalRemain = remainMoney;
            sizeRemain = remainSize;
            // console.log('shengyu1 '+remainMoney)
            return money
        }
    },
    redbagtest: function (openid,callback) {
        red.getRedBag(openid,function (result) {
            console.log(JSON.stringify(result));

            callback()
        })
    },
    validTime:function (openid) {
        red.getValidTime(openid,function (result) {
            console.log(JSON.stringify(result));
        })
    }
}


// test.argorith()

var i=0
setInterval(function () {
        console.log('测试类', i);
        test.redbagtest('2'+i,function () {
            i++
            console.log(i)
        })
    },
2000)

// for (var i=0;i<10;i++) {
//     setTimeout(function () {
//         console.log('测试类', i);
//         test.redbagtest('2'+i,function () {
//             i++
//             console.log(i)
//         })
//     })
// }

var i=0;
// var i=1;
// var i=2;
// var i=3;
//
// test.redbagtest('2'+i,function () {
//             i++
//             console.log(i)
//         })